package com.ropen.sso.configuration;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * ^_^
 * 
 * @author Roy
 * 
 */
public class ConfigRobot {
	
	static Logger log = LoggerFactory.getLogger(ConfigRobot.class);
	
	private Map<String, String> config = new HashMap<String, String>();
	
	private String systemDir;
	
	private ConfigRobot() {}

	private static class ConfigRobotHandler {
		private final static ConfigRobot instance = new ConfigRobot();
	}

	public static ConfigRobot getInstance() {
		return ConfigRobotHandler.instance;
	}

	public String getSystemDir() {
		return systemDir;
	}

	public void setSystemDir(String systemDir) {
		this.systemDir = systemDir;
	}
	
	@SuppressWarnings("rawtypes")
	public void initSystemConfiguration(){
		
		String systemDir = new File("").getAbsolutePath();
		File confDir = new File(systemDir+"/conf");
		if(confDir.exists()&&confDir.isDirectory()){
			File[] files = confDir.listFiles(new FileFilter() {
				public boolean accept(File pathname) {
					if(pathname.getName().endsWith(".prop")){
						return true;
					}
					return false;
				}
			});
			if(files!=null&&files.length>0){
				Properties prop = new Properties();
				for(File f:files){
					try {
						prop.clear();
						InputStream in = new FileInputStream(f);
						prop.load(in);
						Set keyValue = prop.keySet();
						for (Iterator it = keyValue.iterator(); it.hasNext();) {
							String key = (String) it.next();
							config.put(key, (String) prop.get(key));
						}
					}catch(Exception e){
						//TODO EXCEPTION 
						e.printStackTrace();
					}
				}
			}
		}
	}
	
	public String getProperty(String key){
		return config.get(key);
	}
	
	/**
	 * print all properties
	 */
	public void printAllProperties(){
		
	}
}
